﻿using Infinity.BusinessLayer;
using Infinity.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using PagedList.Mvc;

namespace Infinity.Areas.Managerial.Controllers
{
    public class NationalityController : Controller
    {
        InfinityEntities context = new InfinityEntities();

        // GET: /Managerial/Nationality/

        public ActionResult Index(int? page)
        {
            PagedList.IPagedList<NationalityViewModel> list = context.Nationalities.Select(n => new NationalityViewModel
            {
                NationalityID = n.NationalityID,
                NationalityName = n.NationalityName
            }).ToList().ToPagedList(page ?? 1, 4);
            return View(list);
        }

        [HttpPost]
        public ActionResult Index(string searchBy, string searchfor, int? page)
        {

            PagedList.IPagedList<NationalityViewModel> list = context.Nationalities.Select(c => new NationalityViewModel
            {
                NationalityName = c.NationalityName,

                NationalityID = c.NationalityID
            }).ToList().ToPagedList(page ?? 1, 4);

            if (searchBy == "name")
            {
                list = list.Where(i => i.NationalityName.ToLower().Contains(searchfor.ToLower()) || searchfor == null).ToList().ToPagedList(page ?? 1, 4);
            }
           
            return View(list);
        }
        
        public ActionResult EditeNationality(decimal nID)
        {
            
                Nationality nat = context.Nationalities.Where(c => c.NationalityID == nID).FirstOrDefault();
                NationalityViewModel nationality = new NationalityViewModel
                {
                    NationalityID = nat.NationalityID,
                    NationalityName = nat.NationalityName
                };
                return View(nationality);
            
        }

        [ValidateAntiForgeryToken]
        public ActionResult UpdateNationality(NationalityViewModel nationality)
        {
                context.SP_UpdateNationality(nationality.NationalityID, nationality.NationalityName);
                return View("EditeNationality", nationality);
        } 

        public ActionResult DeleteNationality(int ID)
        {
            context.Nationalities.Remove(context.Nationalities.Where(c => c.NationalityID == ID).FirstOrDefault());
           // context.SaveChanges();
            List<NationalityViewModel> list = context.Nationalities.Select(n => new NationalityViewModel
            {
                NationalityID = n.NationalityID,
                NationalityName = n.NationalityName
            }).ToList();
            return View("_NationalityList", list);
        }
  
        public ActionResult NewNationality()
        {

            NationalityViewModel nat = new NationalityViewModel();

            nat.NationalityID = DateTime.Now.Ticks;
            return View(nat);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult NewNationality(NationalityViewModel nat)
        {
            if (ModelState.IsValid)
            {
                Nationality c = new Nationality();
                c.NationalityID = nat.NationalityID;
                c.NationalityName = nat.NationalityName;
                context.Nationalities.Add(c);
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            else return View(nat);
        }

    }
}